package com.example.volunteer.controller;

import com.example.volunteer.exception.CustomException;
import com.example.volunteer.model.Apply;
import com.example.volunteer.service.ApplyService;
import com.example.volunteer.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/api/applications")
public class ApplyController {

    private final ApplyService applyService;

    @Autowired
    public ApplyController(ApplyService applyService) {
        this.applyService = applyService;
    }

    @PostMapping("/apply")
    @PreAuthorize("hasRole('VOLUNTEER')")
    public ResponseEntity<String> applyForActivity(
            @RequestParam Integer activityId,
            @RequestParam String volunteerId) {
        try {
            applyService.applyActivity(activityId, volunteerId);
            return ResponseEntity.ok("已报名");
        } catch (CustomException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("报名失败: " + e.getMessage());
        }
    }

    @PutMapping("/approve")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<String> approveApplication(
            @RequestParam Integer activityId,
            @RequestParam String volunteerId) {
        try {
            applyService.approveApplication(activityId, volunteerId);
            return ResponseEntity.ok("审核通过");
        } catch (CustomException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("审核操作失败: " + e.getMessage());
        }
    }

    @PutMapping("/reject")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<String> rejectApplication(
            @RequestParam Integer activityId,
            @RequestParam String volunteerId) {
        try {
            applyService.rejectApplication(activityId, volunteerId);
            return ResponseEntity.ok("审核拒绝");
        } catch (CustomException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("审核操作失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/delete")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<String> deleteApplication(
            @RequestParam Integer activityId,
            @RequestParam String volunteerId) {
        try {
            applyService.deleteApplication(activityId, volunteerId);
            return ResponseEntity.ok("删除成功");
        } catch (CustomException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("删除失败: " + e.getMessage());
        }
    }

    @PostMapping("/update")
    @PreAuthorize("hasAnyRole('ADMIN')")
    public ResponseEntity<String> updateApply(@RequestBody Apply apply) {
        try {
            applyService.update(apply);
            return ResponseEntity.ok("更新成功");
        } catch (CustomException e) {
            return ResponseEntity.badRequest().body("更新失败: " + e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("更新失败: " + e.getMessage());
        }
    }

    @GetMapping()
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<PageResult<Apply>> getApplicationsWithPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(applyService.findAppliesWithPage(pageNum, pageSize));
    }

    @GetMapping("/volunteerId")
    @PreAuthorize("hasAnyRole('ADMIN', 'VOLUNTEER')")
    public ResponseEntity<PageResult<Apply>> getApplicationsByVolunteerWithPage(
            @RequestParam String volunteerId,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(applyService.getApplicationsByVolunteerWithPage(volunteerId, pageNum, pageSize));
    }

    @GetMapping("/activityId")
    @PreAuthorize("hasAnyRole('ADMIN', 'VOLUNTEER')")
    public ResponseEntity<PageResult<Apply>> getApplicationsByActivityWithPage(
            @RequestParam Integer activityId,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(applyService.getApplicationsByActivityWithPage(activityId, pageNum, pageSize));
    }

    @GetMapping("/status")
    @PreAuthorize("hasAnyRole('ADMIN', 'VOLUNTEER')")
    public ResponseEntity<PageResult<Apply>> getApplicationsByStatusWithPage(
            @RequestParam String status,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(applyService.getApplicationsByStatusWithPage(status, pageNum, pageSize));
    }

    @GetMapping("/time")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<PageResult<Apply>> getApplicationByTimeWithPage(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date time,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(applyService.getApplicationByTimeWithPage(time, pageNum, pageSize));
    }
}
